package com.x.Part5_接口.CompareTest;

import java.lang.annotation.ElementType;

/**
 * ClassName: CompareCircle
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.03.05 19:56
 */
public class CompareCircle extends Circle implements CompareObject{

    public CompareCircle() {
    }

    public CompareCircle(double radius) {
        super(radius);
    }

    //返回值为0；若返回正数，则当前数大；若返回负数，则传入数大
    @Override
    public int CompareTo(Object o) {
        if(this == o)
            return 0;

        if(o instanceof CompareCircle) {
            CompareCircle c = (CompareCircle) o;

            //return (int) (this.getRadius() - c.getRadius());
            //会导致精度问题，因为double类型不能相减后直接强制转int
            if(this.getRadius() > c.getRadius())
                return 1;
            else if(this.getRadius()< c.getRadius())
                return -1;
            else
                return 0;

            //写法二，使用包装类
            // return Double.compare(this.getRadius(), c.getRadius());
        }
        //其实这里应该是异常处理，这里的return 0是随便写的
        //return 0;
        throw new RuntimeException("输入不匹配");
    }

}
